﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Controls
{
    public partial class PButton : Control, IButtonControl
    {
        private int _cornerRedius;
        private float _borderThickNess;
        private bool _isMouseOver = false;
        private Color _borderColor;
        private Color _mouseOverBorderColor;
        private Color _mouseOverBackColor;
        private Color _mouseDownBackColor;
        private Color _mouseDownBorderColor;
        private Color _disabledBackColor;
        private StateType _stateType = StateType.Default;

        #region 显示在属性栏上的一些属性
        [Browsable(true), DefaultValue(2), Description("圆角的半径")]
        [Category("Appearance")]
        public int CornerRedius
        {
            set { _cornerRedius = value; }
            get { return _cornerRedius; }
        }

        [Browsable(true), DefaultValue(1.5f), Description("边框的粗细")]
        [Category("Appearance")]
        public float BorderThickness
        {
            set { _borderThickNess = value; }
            get { return _borderThickNess; }
        }
       
        [Browsable(true), Description("边框颜色")]
        [Category("Appearance")]
        public Color BorderColor
        {
            set { _borderColor = value; }
            get { return _borderColor; }
        }

        [Browsable(true), Description("鼠标进入时的边框颜色")]
        [Category("Appearance")]
        public Color MouseOverBorderColor
        {
            set { _mouseOverBorderColor = value; }
            get { return _mouseOverBorderColor; }
        }

        [Browsable(true), Description("鼠标进入时的背景颜色")]
        [Category("Appearance")]
        public Color MouseOverBackColor
        {
            get { return _mouseOverBackColor; }
            set { _mouseOverBackColor = value; }
        }

        [Browsable(true), Description("鼠标点击时的背景颜色")]
        [Category("Appearance")]
        public Color MouseDownBackColor
        {
            get { return _mouseDownBackColor; }
            set { _mouseDownBackColor = value; }
        }

        [Browsable(true),Description("鼠标点击时的边框颜色")]
        [Category("Appearance")]
        public Color MouseDownBorderColor
        {
            get { return _mouseDownBorderColor; }
            set { _mouseDownBorderColor = value; }
        }


        [Browsable(true),Description("控件Disabed时的背景颜色")]
        [Category("Appearance")]
        public Color DisabledBackColor
        {
            get { return _disabledBackColor; }
            set { _disabledBackColor = value; } 
        }

        public DialogResult DialogResult { get; set; } = DialogResult.None;
        #endregion

        public PButton()
        {
            InitializeComponent();
            CornerRedius = 2;
            Size = new Size(105, 50);
            BackColor = Color.White;
            ForeColor = Color.Black;
            BorderColor = Color.FromArgb(57, 169, 220);
            MouseOverBorderColor = Color.FromArgb(78, 190, 241);
            MouseOverBackColor = Color.FromArgb(251, 251, 251);
            MouseDownBackColor = Color.FromArgb(253, 253, 253);
            MouseDownBorderColor = Color.FromArgb(130, 239, 255);
            DisabledBackColor = Color.LightGray;

            BorderThickness = 1.5f;
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            Cursor = Cursors.Hand;
        }

        /// <summary>
        /// 圆角矩形的path
        /// </summary>
        private GraphicsPath GetGraphicsPath(RectangleF rc)
        {
            float x = rc.X, y = rc.Y, w = rc.Width, h = rc.Height;
            var path = new GraphicsPath();
            path.AddArc(x, y, CornerRedius, CornerRedius, 180, 90); // 左上角圆弧
            path.AddArc(x + w - CornerRedius, y, CornerRedius, CornerRedius, 270, 90); // 右上角圆弧
            path.AddArc(x + w - CornerRedius, y + h - CornerRedius, CornerRedius, CornerRedius, 0, 90); // 右下角圆弧
            path.AddArc(x, y + h - CornerRedius, CornerRedius, CornerRedius, 90, 90); // 左下角圆弧
            path.CloseFigure(); // 闭合
            return path;
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            var btnOff = 0;
            var shadowOff = 0;
            Color borderColor = BorderColor;
            Color backColor = BackColor;
            switch (_stateType)
            {
                case StateType.Leave:
                    break;
                case StateType.Up:
                    backColor = MouseOverBackColor;
                    borderColor = MouseOverBorderColor;
                    break;
                case StateType.Enter:
                    borderColor = MouseOverBorderColor;
                    backColor = MouseOverBackColor;
                    break;
                case StateType.Click:
                    backColor = MouseDownBackColor;
                    borderColor = MouseDownBorderColor;
                    //btnOff = 2;
                    break;
            }

            if (!Enabled)
            {
                backColor = DisabledBackColor;
                borderColor = DisabledBackColor;
            }

            var gra = pe.Graphics;
            gra.Clear(SystemColors.ButtonFace);
            gra.SmoothingMode = SmoothingMode.AntiAlias;

            var rc1 = new RectangleF(btnOff + BorderThickness, btnOff + BorderThickness, ClientSize.Width - btnOff - BorderThickness * 2, ClientSize.Height - btnOff - 2 * BorderThickness);
            var path1 = GetGraphicsPath(rc1);

            #region 绘制边框以及中心
            if (BorderThickness != 0)
            {
                Pen p = new Pen(borderColor)
                {
                    Color = borderColor,
                    Width = BorderThickness
                };
                gra.DrawPath(p, path1);     //边框
            }

            var br1 = new LinearGradientBrush(new PointF(0, 0), new PointF(0, rc1.Height + 6), backColor, backColor);
            gra.FillPath(br1, path1);   //中心
            #endregion

            #region 绘制阴影(按钮有效区)
            // 创建按钮阴影
            var rc2 = new Rectangle(btnOff, btnOff, ClientSize.Width - btnOff, ClientSize.Height - btnOff);
            rc2.Offset(shadowOff, shadowOff);
            var path2 = GetGraphicsPath(rc2);
            // 将region赋值给button
            var rgn = new Region(path1);
            rgn.Union(path2);
            Region = rgn;
            #endregion
            
            #region 绘制文字
            int R = DisabledBackColor.R + 68 > 255 ? 255 : DisabledBackColor.R;
            int G = DisabledBackColor.G + 68 > 255 ? 255 : DisabledBackColor.G;
            int B = DisabledBackColor.B + 68 > 255 ? 255 : DisabledBackColor.B;
            Color foreColor = Enabled ? ForeColor : Color.FromArgb(R,G,B);
            SizeF sizef = gra.MeasureString(Text, Font);
            SolidBrush brush = new SolidBrush(foreColor);
            //RectangleF rectangleF = new RectangleF();
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            gra.DrawString(Text, Font, brush, this.ClientRectangle, sf);
            //gra.DrawString(Text, Font, brush, new PointF((ScreenWidth - sizef.ScreenWidth) / 2 + 1.5f, (ScreenHeight - sizef.ScreenHeight) / 2 + 2f));
            #endregion
            //base.OnPaint(pe);
        }

        protected override void OnEnabledChanged(EventArgs e)
        {
            Invalidate();
            base.OnEnabledChanged(e);
        }

        protected override void OnMouseDown(MouseEventArgs eventArgs)
        {
            _stateType = StateType.Click;
            Invalidate();
            base.OnMouseDown(eventArgs);
        }

        protected override void OnMouseUp(MouseEventArgs eventArgs)
        {
            _stateType = _isMouseOver ?  StateType.Up : StateType.Leave;
            Invalidate();
            base.OnMouseUp(eventArgs);
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            _stateType = StateType.Enter;
            _isMouseOver = true;
            Invalidate();
            base.OnMouseEnter(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            _stateType = StateType.Leave;
            _isMouseOver = false;
            Invalidate();
            base.OnMouseLeave(e);
        }

        protected override void OnTextChanged(EventArgs e)
        {
            _stateType = Enabled ? StateType.Default : StateType.Disabled;
            Invalidate();
            base.OnTextChanged(e);
        }

        /// <summary>
        /// 通知控件它是默认按钮
        /// </summary>
        public void NotifyDefault(bool value)
        {

        }

        /// <summary>
        /// 调用点击事件
        /// </summary>
        public void PerformClick()
        {
            if (CanSelect && Enabled)
            {
                OnClick(EventArgs.Empty);
            }
        }
    }
}
